local hunzi = fk.CreateSkill {

  name = "joyex__hunzi",

  tags = { Skill.Wake, },

}



hunzi:addEffect(fk.GameStart, {
  name = "joyex__hunzi",
  frequency = Skill.Wake,
  events = {fk.GameStart,fk.HpChanged},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hunzi.name) and
      player:usedSkillTimes(hunzi.name, Player.HistoryGame) == 0
  end,
  can_wake = function(self, event, target, player, data)
    return player.hp == 1
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:hasSkill("joyex__zhiba") and player.role == "lord" and #room.players >= 5 then lord = true end
    if player.general == "joyex__sunce" then 
      room:notifySkillInvoked(player, "joyex__hunzi","drawcard")
      room:changeHero(player,"joy_skin__sunce",false,false,true,false)
      if lord then room:handleAddLoseSkills(player,"joyex__zhiba",nil) end
    elseif player.deputyGeneral == "joyex__sunce" then
      room:notifySkillInvoked(player, "joyex__hunzi","drawcard")
      room:changeHero(player,"joy_skin__sunce",false,true,true,false)
      if lord then room:handleAddLoseSkills(player,"joyex__zhiba",nil) end
    end
    room:changeMaxHp(player, -1)
    room:handleAddLoseSkills(player, "ex__yingzi|joy__yinghun", nil, true, false)
  end,
})
hunzi:addEffect(fk.HpChanged, {
  name = "joyex__hunzi",
  frequency = Skill.Wake,
  events = {fk.GameStart,fk.HpChanged},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hunzi.name) and
      player:usedSkillTimes(hunzi.name, Player.HistoryGame) == 0
  end,
  can_wake = function(self, event, target, player, data)
    return player.hp == 1
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:hasSkill("joyex__zhiba") and player.role == "lord" and #room.players >= 5 then lord = true end
    if player.general == "joyex__sunce" then 
      room:notifySkillInvoked(player, "joyex__hunzi","drawcard")
      room:changeHero(player,"joy_skin__sunce",false,false,true,false)
      if lord then room:handleAddLoseSkills(player,"joyex__zhiba",nil) end
    elseif player.deputyGeneral == "joyex__sunce" then
      room:notifySkillInvoked(player, "joyex__hunzi","drawcard")
      room:changeHero(player,"joy_skin__sunce",false,true,true,false)
      if lord then room:handleAddLoseSkills(player,"joyex__zhiba",nil) end
    end
    room:changeMaxHp(player, -1)
    room:handleAddLoseSkills(player, "ex__yingzi|joy__yinghun", nil, true, false)
  end,
})

return hunzi